const app = getApp();

import { getIndexData, getCoupons, getTemlIds, getLiveList } from '../../api/api.js';
import { CACHE_SUBSCRIBE_MESSAGE } from '../../config.js';
import Util from '../../utils/util.js';
import wxh from '../../utils/wxh.js';

import { getProductHotRow } from '../../api/store.js';

import { editAddressCateByMap, getAddressDetailCateByMap } from '../../api/user.js';
var QQMapWX = require('../../libs/maps/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: 'P4FBZ-LGPKF-WMVJ7-NOKCC-RH3K6-BSFRZ' // 必填
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    itemNew: [],
    activityList: [],
    menus: [],
    bastBanner: [],
    bastInfo: '',
    bastList: [],
    fastInfo: '',
    fastList: [],
    firstInfo: '',
    firstList: [],
    salesInfo: '',
    likeInfo: [],
    lovelyBanner: {},
    benefit: [],
    indicatorDots: false,
    circular: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    parameter: {
      'navbar': '0',
      'return': '0'
    },
    window: false,
    iShidden: false,
    navH: "",
    newGoodsBananr: '',
    selfLongitude: '',
    selfLatitude: '',
    liveList: [],
    liveInfo: {},
    /*热门推荐 */
    host_product:[],
    page: 1,
    limit: 4,
    row_count:0,
    /**navbar样式 */
    navH:0,
  },
  closeTip: function () {
    wx.setStorageSync('msg_key', true);
    this.setData({
      iShidden: true
    })
  },
  /**跳商品列表 */
  goGoodsList:function(e){
    var index=e.currentTarget.dataset.index;
    console.log('index',index);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**设置navbar */
    this.setData({
      navH: app.globalData.navHeight
    });
    /**设置navbar */
    wxh.selfLocation(1);
    this.setData({
      navH: app.globalData.navHeight
    });
    if (options.spid) app.globalData.spid = options.spid;
    if (options.scene) app.globalData.code = decodeURIComponent(options.scene);
    if (wx.getStorageSync('msg_key')) this.setData({ iShidden: true });

    this.get_host_product(0);
    //得到经纬度
  },
  editAddressCateByMap: function (value) {
    // console.log('value的值',value);
    // var value=JSON.parse(JSON.stringify(value));
    // console.log('value的值后面',value);
    console.log('value',value);
    editAddressCateByMap(value).then(res => {
     console.log('res的值',res);
    }).catch(err => {
      return app.Tips({ title: err });
    })
  },
  
  /**
  * 获取我的推荐
  */
  get_host_product: function (page = 0) {
    var that = this;
    getProductHotRow(that.data.page,that.data.limit).then(res => {
      console.log('热门',res.data);
    
      if (page == 0) {
        that.setData({ host_product: res.data.productHot });
      } else {
        that.setData({ host_product: that.data.host_product.concat(res.data.productHot) });
      }
      that.setData({row_count: res.data.row_count});
    });
  },
  catchTouchMove: function (res) {
    return false
  },
  onColse: function () {
    this.setData({ window: false });
  },
  getProvinceByMap(){
    /**只执行一次的方法 */
    const _this = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude

        console.log('latitude', latitude);
        console.log('longitude', longitude);

        demo.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            var address_info = res.result;
            var address_component = address_info.address_component;
            var location = address_info.location;
            console.log('address_info', address_info);
            var value_attr = { detail: address_info.address, province: address_component.province, city: address_component.city, district: address_component.district, longitude: location.lng, latitude: location.lat };
            console.log('value_attr', value_attr);
            _this.editAddressCateByMap(value_attr);
            /**发送post地址 */
          },
          fail: function (res) {
            console.log(res);
          },
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    this.getProvinceByMap();

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getIndexConfig();
    
    /**显示调用 */
    // this.get_host_product(0);
  },
  
  getIndexConfig: function () {
    var that = this;
    getIndexData().then(res => {
      that.setData({
        imgUrls: res.data.banner,
        logoUrl: res.data.logoUrl,
        menus: res.data.menus
      });
      // wx.getSetting({
      //   success(res) {
      //     if (!res.authSetting['scope.userInfo']) {
      //       that.setData({ window: that.data.couponList.length ? true : false });
      //     } else {
      //       that.setData({ window: false, iShidden: true });
      //     }
      //   }
      // });
    })
  },
    goDetail: function (e) {
      let item = e.currentTarget.dataset.items
      if (item.activity && item.activity.type === "1") {
        wx.navigateTo({
          url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
        });
      } else if (item.activity && item.activity.type === "2") {
        wx.navigateTo({ url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}` });
      } else if (item.activity && item.activity.type === "3") {
        wx.navigateTo({
          url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
        });
      } else {
        wx.navigateTo({ url: `/pages/goods_details/index?id=${item.id}` });
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ window: false });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getIndexConfig();
    if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
console.log('下拉');
    let page = this.data.page;
    let self = this;
    if (self.data.host_product.length >= self.data.row_count) {
      return;
    }
    this.data.page++;
    this.get_host_product(page);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})